import time
import asyncio

money = 0


async def add():
    global money
    for i in range(100):
        if i % 3 == 0:
            await asyncio.sleep(0.1)
        money = money + i


async def reduce():
    global money
    for i in range(100):
        if i % 5 == 0:
            await asyncio.sleep(0.1)
        money = money - i


def add_sync():
    global money
    for i in range(100):
        if i % 3 == 0:
           time.sleep(0.1)
        money = money + i


def reduce_sync():
    global money
    for i in range(100):
        if i % 5 == 0:
            time.sleep(0.1)
        money = money - i


if __name__ == '__main__':
    start_time = time.time()
    # loop = asyncio.get_event_loop()
    # # 协程执行只需要3.451164960861206
    # loop.run_until_complete(asyncio.wait([add(), reduce()]))
    # 同步执行需要5.434074401855469
    add_sync()
    reduce_sync()
    print(money)
    print("spend time is {}".format(time.time() - start_time))
